﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

/// <summary>
/// 类似 N GUI 方式控制EventTrigger 事件,EventTrigger 的脚本方式 
/// </summary>
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
    // public delegate void VoidDelegate(GameObject go);
    public delegate void VoidEventDelegate(PointerEventData eventData);
    public delegate void VoidBaseEventDelegate(BaseEventData eventData);
    // public delegate void BoolDelegate(GameObject go, bool state);
    // public delegate void FloatDelegate(GameObject go, float delta);
    // public delegate void VectorDelegate(GameObject go, Vector2 delta);
    // public delegate void ObjectDelegate(GameObject go, GameObject obj);
    // public delegate void KeyCodeDelegate(GameObject go, KeyCode key);

    public VoidEventDelegate onClick;
    public VoidEventDelegate onDown;
    public VoidEventDelegate onEnter;
    public VoidEventDelegate onExit;
    public VoidEventDelegate onUp;
    public VoidBaseEventDelegate onSelect;
    public VoidBaseEventDelegate onUpdateSelect;
    public VoidEventDelegate onBeginDrag;
    public VoidEventDelegate onDrag;
    public VoidEventDelegate onDrop;
    public VoidEventDelegate onEndDrag;


    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    static public EventTriggerListener Get(Transform transform)
    {
        return Get(transform.gameObject);
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(eventData);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(eventData);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(eventData);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(eventData);
    }

    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(eventData);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(eventData);

    }
    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(eventData);

    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(eventData);

    }
}